package day18;

import java.util.concurrent.locks.ReentrantLock;

public class HomeWork {
}


class A3 {
    static int distance = 1000;
    static boolean end = false;
    public static void main(String[] args) {
        new Thread(()->{
            String name = Thread.currentThread().getName();
            int num = 0;
            while (num<distance&& !end){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                num+=100;
                if (!end){
                    System.out.println(name + "爬完了100m");
                }
            }
            if (num>=distance){
                System.out.println("胜利者:" + name);
                end = true;
            }
        },"A").start();

        new Thread(()->{
            String name = Thread.currentThread().getName();
            int num1 = 0;
            while (num1<distance&& !end){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                num1+=100;
                if (!end){
                    System.out.println(name + "爬完了100m");
                }
            }
            if (num1>=distance){
                System.out.println("胜利者:"+name);
                end=true;
            }
        },"B").start();
    }
}

class B3 {
    static boolean isNumberTurn = true;
    public static void main(String[] args) {
        ReentrantLock lock=new ReentrantLock(true);
        new Thread(() -> {
            for (int i = 1; i <= 52; i += 2) {
                lock.lock();
                try {
                    while (!isNumberTurn) {
                        lock.unlock();
                        Thread.yield();
                        lock.lock();
                    }
                    System.out.print(i);
                    System.out.print(i + 1);
                    isNumberTurn = false;
                } finally {
                    lock.unlock();
                }
            }
        }).start();
        new Thread(() -> {
            for (char c = 'A'; c <= 'Z'; c++) {
                lock.lock();
                try {
                    while (isNumberTurn) {
                        lock.unlock();
                        Thread.yield();
                        lock.lock();
                    }
                    System.out.print(c);
                    isNumberTurn = true;
                } finally {
                    lock.unlock();
                }
            }
        }).start();
    }
}

//
class B31{
    public static void main(String[] args) {
        new Thread(() -> {
            for (int i = 1; i <= 26; i += 1) {
                synchronized (B31.class){
                    System.out.print((char)i);
                    B31.class.notifyAll();
                        try {
                            B31.class.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                }
            }
        }).start();
    }
}

class B32{
    ReentrantLock lock=new ReentrantLock(true);
    int num = 1;
    int ch = 65;
    public void print(){
        String name = Thread.currentThread().getName();
        lock.lock();
        if("0".equals(name)){
            System.out.print(num+++""+num++);
        }else {
            System.out.print((char)ch);
            ch++;
        }
        lock.unlock();
    }

    public static void main(String[] args) {
        B32 b32 = new B32();
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                for (int j = 1; j <= 26; j++) {
                    b32.print();
                }
            },i+"").start();
        }
    }
}
